package com.lw.system;

import lombok.Getter;
import lombok.Setter;
import org.crazycake.shiro.AuthCachePrincipal;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Getter
@Setter
public class ProfileResult implements AuthCachePrincipal, Serializable {
    private String mobile;
    private String username;
    private String company;
    private String companyId;
    private Map<String,Object> roles=new HashMap<>();
    public ProfileResult(User user){
        this.companyId=user.getCompanyId();
        this.mobile=user.getMobile();
        this.username=user.getUsername();
        this.company=user.getCompanyName();
        List<Role> roles = user.getRoles();
        List<String> menus=new ArrayList<>();
        List<String> points=new ArrayList<>();
        List<String> apis=new ArrayList<>();
        for (Role role:roles){
            List<Permission> permissions = role.getPermissions();
            for (Permission permission:permissions){
                String code = permission.getCode();
                if(permission.getType() == 1) {
                    menus.add(code);
                }else if(permission.getType() == 2) {
                    points.add(code);
                }else {
                    apis.add(code);
                }
            }
        }
        this.roles.put( "menus",menus );
        this.roles.put( "points",points );
        this.roles.put( "apis",apis );
    }
    public ProfileResult(User user,List<Permission> permissions){
        this.companyId=user.getCompanyId();
        this.mobile=user.getMobile();
        this.username=user.getUsername();
        this.company=user.getCompanyName();
        List<String> menus=new ArrayList<>();
        List<String> points=new ArrayList<>();
        List<String> apis=new ArrayList<>();
        for (Permission permission:permissions){
            String code = permission.getCode();
            if(permission.getType() == 1) {
                menus.add(code);
            }else if(permission.getType() == 2) {
                points.add(code);
            }else {
                apis.add(code);
            }
        }
        this.roles.put( "menus",menus );
        this.roles.put( "points",points );
        this.roles.put( "apis",apis );
    }

    @Override
    public String getAuthCacheKey() {
        return null;
    }
}
